<template>
  <ContentWrap>
    <fromTitle title="样品详细信息查询"   :queryParams="queryParams" @search="getList" @reset="resetQuery" /> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >


      <el-form-item label="甲方单位" prop="fistParty">
        <el-input
          v-model="queryParams.fistParty"
          placeholder="请输入甲方单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item  label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item  label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item  label="样品编号" prop="sampleNum">
        <el-input
          v-model="queryParams.sampleNum"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item  label="承检机构" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入承检机构"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="抽样时间" prop="samplingDate">
        <el-date-picker
          v-model="queryParams.samplingDate1"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="检验日期" prop="checkStartTime">
        <el-date-picker
          v-model="queryParams.checkStartTime1"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
    <el-button type="info" @click="handleExport">导出</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="样品名称" align="center" prop="sampleName">
        <template #default="scope">
          <el-link type="primary" @click="handleViewDetails(scope.row)">
            {{ scope.row.sampleName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="样品编号" align="center" prop="sampleNum" />
      <el-table-column label="抽样地点" align="center" prop="samplingSite" />
      <el-table-column label="委托单位" align="center" prop="entrustCompany" />
      <el-table-column label="生产单位" align="center" prop="productionCompany" />
      <el-table-column label="抽样日期" align="center" prop="samplingDate" />
      <el-table-column label="检验状态" align="center" prop="checkStateString" />
      <el-table-column label="检验结果" align="center" prop="checkConclusion" />
      <el-table-column label="承检机构" align="center" prop="orgName" />
      <el-table-column label="报告编号" align="center" prop="reportNum" />
      <el-table-column label="样品来源" align="center" prop="samplingTypeString" />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrganizationQualificationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrganizationQualificationApi, OrganizationQualificationVO } from '@/api/qis/organizationqualification'
import OrganizationQualificationForm from './OrganizationQualificationForm.vue'
import { SampleInformationApi } from '@/api/qis/sampleinformation'


const router = useRouter()
/** 质检平台-承检资质 列表 */
defineOptions({ name: 'OrganizationQualification' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrganizationQualificationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type:4
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  queryParams.samplingStartDate =queryParams.samplingDate1 ?   queryParams.samplingDate1[0] :''
queryParams.samplingEndDate = queryParams.samplingDate1?  queryParams.samplingDate1[1] :''
queryParams.checkFilterStartTime = queryParams.checkStartTime1 ?  queryParams.checkStartTime1[0] :''
queryParams.checkFilterEndTime =  queryParams.checkStartTime1?  queryParams.checkStartTime1[1] :''

  try {
    const data = await SampleInformationApi.getSampleInformationPage(queryParams)
    list.value = data.list
    list.value.forEach((item) => {
      item.selected = false
    })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleViewDetails = (row: any) => {
  router.push({path:'/inspection-info-management/sampleInformationApproval/details',query:{id:row.id}})
}
const arrayList = ref([])
const handleSelect = (row ) => {
  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(e=>e !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.samplingDate1 = undefined
  queryParams.checkStartTime1 = undefined
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SampleInformationApi.deleteSampleInformation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    arrayList.value = []
    list.value.forEach(e=>{
    e.selected  = false
    
   })
     getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {

  try {
    if(arrayList.value.length > 0 ){
          // 导出的二次确认
        
    await message.exportConfirm()
    loading.value = true
    const idsArr = arrayList.value.join(';')
    // 发起导出
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation({ids:idsArr,...queryParams})
    download.excel(data, '质检平台-样品详细信息查询.xls')
    
    }else {
          // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    loading.value = true
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation(queryParams)
    download.excel(data, '质检平台-样品详细信息查询.xls')
    }

  } catch {
  } finally {
    loading.value = false
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})


onDeactivated(()=>{
  resetQuery()
})
</script>


<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>